<?php
require_once('MySqlConnection.php');
require_once('Blog.php');

class BlogDAL{

	private $_connection;
	
	//SINGLETON PATTERN
    private static $_instance = null;
	public static function Instance(){
		if (self::$_instance == null) self::$_instance = new BlogDAL();
		return self::$_instance;
	}
	//SINGLETON PATTERN END
	
	private function __construct(){
		include_once('./include/db.inc');
		$this->_connection = MySqlConnection::Instance($server,$user,$password,$database);
	}
	
	public function getBlogs(){
		$this->_connection->Open();
		$sqlString = "SELECT id, title, body, unix_timestamp(lastedit) AS lastedit, user_id FROM blog ORDER BY lastedit DESC";
		$resultData = $this->_connection->ExecuteSQL($sqlString, 'O');
		return $this->Mapping($resultData, 'blog');
	}
	
	public function getBlogsForUser($userId){
		$this->_connection->Open();
		$sqlString = "SELECT id, title, body, unix_timestamp(lastedit) AS lastedit, user_id FROM blog WHERE user_id = '{$userId}' ORDER BY lastedit DESC";
		$resultData = $this->_connection->ExecuteSQL($sqlString, 'O');
		return $this->Mapping($resultData, 'blog');
	}
	
	public function getBlogsForPractice($practiceId){
		$this->_connection->Open();
		$sqlString = "SELECT DISTINCT b.id AS id, b.title AS title, b.body AS body, unix_timestamp(lastedit) AS lastedit, b.user_id AS user_id FROM blog b JOIN practice p ON p.user_id = b.user_id WHERE p.id = '{$practiceId}' ORDER BY lastedit DESC";
		$resultData = $this->_connection->ExecuteSQL($sqlString, 'O');
		return $this->Mapping($resultData, 'blog');
	}
	
	public function addBlog($blog){
		include('./include/db.inc');
		$mysqli = new mysqli($server, $user, $password, $database);
		if(mysqli_connect_errno()) {
		  echo "Connection Failed: " . mysqli_connect_errno();
		  exit();
		}
		if($stmt = $mysqli->prepare("INSERT INTO blog(title,body,user_id) VALUES(?,?,?)")) {
			$stmt->bind_param("ssi",$blog['title'],$blog['body'],$blog['user_id']);
			$success = $stmt->execute();
			if(!$success){
				echo "Inserting blog failed!";
			}
		}
		else{
			
			echo "Failed preparing insert blog statement!";
		}
		$mysqli -> close();
	}
	
	public function removeBlog($blogId){
		include('./include/db.inc');
		$mysqli = new mysqli($server, $user, $password, $database);
		if(mysqli_connect_errno()) {
		  echo "Connection Failed: " . mysqli_connect_errno();
		  exit();
		}
		if($stmt = $mysqli->prepare("DELETE FROM blog WHERE id = ?")) {
			$stmt->bind_param("i", $blogId);
			$success = $stmt->execute();
			if(!$success){
				echo "Removing blog failed!";
			}
		}
		else{
			echo "Failed preparing delete blog statement!";
		}
		$mysqli -> close();
	}
	
	public function updateBlog($blog) {
		include('./include/db.inc');
		$mysqli = new mysqli($server, $user, $password, $database);
		if(mysqli_connect_errno()) {
		  echo "Connection Failed: " . mysqli_connect_errno();
		  exit();
		}
		if($stmt = $mysqli->prepare("UPDATE blog SET title = ?, body = ? WHERE id = ?")) {
			$stmt->bind_param("ssi", $blog['title'], $blog['body'], $blog['id']);
			$success = $stmt->execute();
			if(!$success){
				echo "Updating blog failed!";
			}
		}
		else{
			echo "Failed preparing update blog statement!";
		}
		$mysqli -> close();
	}
	
	public function getBlogsById($blogId)
	{
		$this->_connection->Open();
		$sqlString = "SELECT id, title, body, unix_timestamp(lastedit) AS lastedit, user_id FROM blog WHERE id = '{$blogId}'";
		$resultData = $this->_connection->ExecuteSQL($sqlString, 'O');
		return $this->Mapping($resultData, 'blog');
	}
	
	//MAPPING	
	private function Mapping($queryData,$resultType){
		foreach($queryData as $data){
			$resultArray[] = $this->Map($data,$resultType);
		}
		return $resultArray;
	}
	
	private function Map($data,$resultType){
		$object = new Blog($data->id,$data->title,$data->body,$data->lastedit,$data->user_id);
		return $object;
	}
	//MAPPING END
	
}

